using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace KaroGame.FrontEnd.Menu
{
    public class OptionsMenu : IMenu
    {
        public OptionsMenu(SpriteFont font)
        {
            Title = "Options";

            MenuElement menuElement;
            Elements = new List<MenuElement>();
            Rectangle rec;
            string title;
            //int index = 0;

            title = "Mouse inverted: ";
            rec = new Rectangle(50, 120, 2000, (int)font.MeasureString(title).Y);
            menuElement = new MenuElement(title, new Vector2(50, 120), MenuElementAction.Nothing, rec);
            menuElement.Options.Add(new MenuElementOption("true"));
            menuElement.Options.Add(new MenuElementOption("false"));
            Elements.Add(menuElement);

            //index = 0;
            title = "Ply depth: ";
            rec = new Rectangle(50, 170, 2000, (int)font.MeasureString(title).Y);
            menuElement = new MenuElement(title, new Vector2(50, 170), MenuElementAction.Nothing, rec);
            menuElement.Options.Add(new MenuElementOption("1"));
            menuElement.Options.Add(new MenuElementOption("2"));
            menuElement.Options.Add(new MenuElementOption("3"));
            menuElement.Options.Add(new MenuElementOption("4"));
            menuElement.Options.Add(new MenuElementOption("5"));
            menuElement.Options.Add(new MenuElementOption("6"));
            menuElement.Options.Add(new MenuElementOption("7"));
            Elements.Add(menuElement);

            title = "HUD Position: ";
            rec = new Rectangle(50, 220, 2000, (int)font.MeasureString(title).Y);
            menuElement = new MenuElement(title, new Vector2(50, 220), MenuElementAction.Nothing, rec);
            menuElement.Options.Add(new MenuElementOption("top"));
            menuElement.Options.Add(new MenuElementOption("bottom"));
            Elements.Add(menuElement);

            title = "Fullscreen: ";
            rec = new Rectangle(50, 270, 2000, (int)font.MeasureString(title).Y);
            menuElement = new MenuElement(title, new Vector2(50, 270), MenuElementAction.Nothing, rec);
            menuElement.Options.Add(new MenuElementOption("true"));
            menuElement.Options.Add(new MenuElementOption("false"));
            Elements.Add(menuElement);

            title = "Resolution: ";
            rec = new Rectangle(50, 320, 2000, (int)font.MeasureString(title).Y);
            menuElement = new MenuElement(title, new Vector2(50, 320), MenuElementAction.Nothing, rec);
            foreach (DisplayMode dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
            {
                if (dm.Format == SurfaceFormat.Bgr32)
                    menuElement.Options.Add(new MenuElementOption(dm.Width + "x" + dm.Height));
            }
            menuElement.SelectedOption = menuElement.Options.Count - 1;
            Elements.Add(menuElement);

            title = "Return";
            rec = new Rectangle(50, 370, 2000, (int)font.MeasureString(title).Y);
            Elements.Add(new MenuElement(title, new Vector2(50, 370), MenuElementAction.Return, rec));

            GetSettings();
        }

        public void GetSettings()
        {
            foreach (MenuElement element in Elements)
            {
                int index = 0;

                if(element.Titel.Contains("Mouse inverted"))
                {
                    foreach (MenuElementOption option in element.Options)
                    {
                        if (option.Value == SettingsManager.Instance.IsMouseInverted.ToString().ToLowerInvariant())
                        {
                            element.SelectedOption = index;
                            break;
                        }
                        index++;
                    }
                }

                if (element.Titel.Contains("Ply depth"))
                {
                    foreach (MenuElementOption option in element.Options)
                    {
                        if (option.Value == SettingsManager.Instance.PlyDepth.ToString())
                        {
                            element.SelectedOption = index;
                            break;
                        }
                        index++;
                    }
                }

                if (element.Titel.Contains("HUD Position"))
                {
                    foreach (MenuElementOption option in element.Options)
                    {
                        if (option.Value == SettingsManager.Instance.HudPosition.ToString().ToLowerInvariant())
                        {
                            element.SelectedOption = index;
                            break;
                        }
                        index++;
                    }
                }

                if (element.Titel.Contains("Fullscreen"))
                {
                    foreach (MenuElementOption option in element.Options)
                    {
                        if (option.Value == SettingsManager.Instance.Fullscreen.ToString().ToLowerInvariant())
                        {
                            element.SelectedOption = index;
                            break;
                        }
                        index++;
                    }
                }

                if (element.Titel.Contains("Resolution"))
                {
                    foreach (MenuElementOption option in element.Options)
                    {
                        if (option.Value == (SettingsManager.Instance.ResolutionWidth + "x" + SettingsManager.Instance.ResolutionHeight))
                        {
                            element.SelectedOption = index;
                            break;
                        }
                        index++;
                    }
                }
            }
        }

        #region IMenu Members

        public string Title { get; set; }

        public List<MenuElement> Elements { get; set; }

        #endregion
    }
}